/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package bankingsystem;

import java.util.Calendar;

/**
 * Defines the CurrentAccount object that models a current account.
 * @author Gokul
 * @vesion 1.0
 */

public class CurrentAccount extends Account{

    /*defining variables. the overdraftLimit float variable represents the maximum threshold upto
     which the account could be overdrawn and the checksDrawn integer variable keeps tracj of the 
     number of checks drawn on the account.*/
    private float overdraftLimit;
    private int checksDrawn;

    /**
     * Template for a new CurrentAccount object.
     * @param accountNo the number of the account
     * @param customer the customer who holds this account, represented as a Customer object
     * @param balance the account balance
     * @param startDate the date on which the account was started
     * @param overdraftLimit the limit for which the account could be overdrawn
     */
    CurrentAccount(String accountNo,Customer customer,float balance,Calendar startDate, float overdraftLimit){
        /*referring to the constructor of the superclass*/
       super(accountNo, customer, balance, startDate);
       /*initializing local variables*/
       this.overdraftLimit = overdraftLimit;
       this.checksDrawn = 0;
    }
    /**
     * Withdraw a specified amount from the account, with validation for
     * negative values, overdraft limit etc..
     *
     * @param f the amount that is withdrawn
     */
    public boolean withdraw(float f){
        boolean status; /*flag for checking if the transaction succeeded or not.*/
        if(this.balance>f){
            /*if the entered amount is within limits, do the transaction*/
            this.balance -= f;

            status = true;  /*success*/

        }
        else{
            /*if balance goes less than zero:*/
            Float tempAmt = f;
            Float tempBal = this.balance;
            tempAmt -= tempBal;
            
            if(this.overdraftLimit>tempAmt){
                /*proceed if overdraft limit is not exceeded.*/
                System.out.println("Insufficient funds. An overdraft of "+tempAmt+" has been taken.");
                this.balance -= f;

                status = true;  /*success*/

            }
            else{
                /*or warn the usr of insufficient funds.*/
                System.out.println("Error: insufficient funds, and overdraft limit exceeded.");
                status = false; /*failed*/

            }
        }
        return status;
    }

    /**
     * Deposit the specified amount to the account
     * @param f the amount that is being deposited.
     */
    public void deposit(float f){
        this.balance += f;
    }

    /**
     * Writing a check for a specified amount
     * @param f the amount for which the check is written.
     */
    public void writeCheck(float f){
        withdraw(f);
        this.checksDrawn++;
    }

    /**
     * Show the details of the particular CurrentAccount object
     * on which this particular method is called.
     */
    
    @Override
    public void show(){
        System.out.println("\n--Current account "+this.accountNo+" details:--"
                            +"\nHolder ID: "+this.customer.getCustomerID()
                            +"\nBalance: "+Float.toString(this.balance)
                            +"\nOverdraft limit: "+Float.toString(this.overdraftLimit)
                            +"\nChecks drawn: "+Integer.toString(this.checksDrawn)

                            );
    }
    /**
     * Used to obtain the account number of the current CurrentAccount class.
     * @return the account number.
     */
    public String getAccountNo(){
        return this.accountNo;
    }
}
